package com.example.campus.dao.table_gateway;

import com.example.campus.dao.model.Activity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;


@Mapper
public interface ActivityMapper {

    @Insert("insert into t_manager_activity " +
            "(name, category, title, creator, abstract, detailed_desc, created_time, archived_time, start_time, end_time, status) " +
            "VAlUES " +
            "(#{name}, #{category}, #{title}, #{creator}, #{abstraction}, #{detailedDesc}, #{createdTime}, #{archivedTime}, #{startTime}, #{endTime}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addActivity(Activity activity);

    @Select("Select * from t_manager_activity")
    Set<Activity> allActivities();

    @Select("Select * from t_manager_activity where id=#{id}")
    Activity getActivityById(@Param("id") long id);

    @Select("Select * from t_manager_activity where `name` like #{name} order by created_time desc")
    List<Activity> getAllActivitiesByNameLike(@Param("name") String  namePattern);

    @Update("<script>" +
            "UPDATE t_manager_activity " +
            "<set>" +
                "<if test='name != null'>name = #{name}, </if>"+
                "<if test='category != null'>category = #{category}, </if>"+
                "<if test='title != null'>title = #{title}, </if>"+
                "<if test='creator != null'>creator = #{creator}, </if>"+
                "<if test='abstraction != null'>abstract = #{abstraction}, </if>"+
                "<if test='detailedDesc != null'>detailed_desc = #{detailedDesc}, </if>"+
                "<if test='picUrls != null'>pic_urls = #{picUrls}, </if>"+
                "<if test='createdTime != null'>created_time = #{createdTime}, </if>"+
                "<if test='archivedTime != null'>archived_time = #{archivedTime}, </if>"+
                "<if test='startTime != null'>start_time = #{startTime}, </if>"+
                "<if test='endTime != null'>end_time = #{endTime}, </if>"+
                "<if test='status != null'>status = #{status} </if>"+
            "</set>"+
            "where id = #{id}"+
            "</script>")
    void updateActivity(Activity activity);

    @Delete("delete from t_manager_activity where id = #{id}")
    void  deleteActivityById(@Param("id") long id);
}
